// mmo_game_server/server_management/load_balancer.go
package server_management

import (
    "math/rand"
    "sync"
    "time"
)

// LoadBalancer 负载均衡器
type LoadBalancer struct {
    servers []string
    mu      sync.Mutex
}

// NewLoadBalancer 创建新的负载均衡器
func NewLoadBalancer() *LoadBalancer {
    rand.Seed(time.Now().UnixNano())
    return &LoadBalancer{}
}

// AddServer 添加服务器到负载均衡器
func (lb *LoadBalancer) AddServer(server string) {
    lb.mu.Lock()
    defer lb.mu.Unlock()
    lb.servers = append(lb.servers, server)
}

// GetServer 获取一个服务器
func (lb *LoadBalancer) GetServer() string {
    lb.mu.Lock()
    defer lb.mu.Unlock()
    if len(lb.servers) == 0 {
        return ""
    }
    return lb.servers[rand.Intn(len(lb.servers))]
}